﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using IGolovkine.Core.Diagnostics;
using System.ServiceModel;

namespace IGolovkine.ShoutCastRipper.Service.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            var properties = ServiceConfigurationManager.LoadServiceProperties();

            using (IStationRipperService service = new StationRipperService(properties))
            using (AdminService adminService = new AdminService(service))
            using (ServiceHost serviceHost = new ServiceHost(adminService))
            {
                serviceHost.Open();
                service.Start();
                global::System.Console.ReadLine();
            }
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            DumpException(ex);
        }

        private static void DumpException(Exception ex)
        {
            StringBuilder buffer = new StringBuilder();
            buffer.AppendLine(ObjectDumper.Format(ex));
            string fileName = string.Format("CurrentDomain_UnhandledException_{0:yyyy_MMM_dd_HH_mm_ss}.txt", DateTime.Now);
            File.WriteAllText(fileName, buffer.ToString());
        }
    }
}
